var idxlist = [];
/*alert(edittype);*/
//edittype==1为发车端 2为承运端
$.get('/admin/getIdxFiles?edittype='+edittype, function(result){
    idxlist = JSON.parse(result);
    if(idxlist.length>0){

        if(edittype==1){
            $("#slide0").attr("src",'/javascripts/slider/img/sls/'+idxlist[0]);//设置预览
            $("#slide1").attr("src",'/javascripts/slider/img/sls/'+idxlist[1]);//设置预览
            $("#slide2").attr("src",'/javascripts/slider/img/sls/'+idxlist[2]);//设置预览
            $("#slide3").attr("src",'/javascripts/slider/img/sls/'+idxlist[3]);//设置预览
        }else{
            $("#slide0").attr("src",'/javascripts/slider/img/sls1/'+idxlist[0]);//设置预览
            $("#slide1").attr("src",'/javascripts/slider/img/sls1/'+idxlist[1]);//设置预览
            $("#slide2").attr("src",'/javascripts/slider/img/sls1/'+idxlist[2]);//设置预览
            $("#slide3").attr("src",'/javascripts/slider/img/sls1/'+idxlist[3]);//设置预览
        }
    }

});


function editVoice(index){//编辑商家信息

    //动态加载
    var imgWindow = $('<div/>').dialog(
        {
            width : 800,
            height : 430,
            href : '/admin/views/appIdxUpload.html',
            modal : true,
            title : '编辑首页图片轮播',
            maximizable:true,
            cache:false,
            minimized:true,
            buttons : [{
                text : '取&nbsp;&nbsp;消',
                width : 60,
                handler : function() {
                    imgWindow.dialog('close');
                }
            },
                {
                    text : '保&nbsp;&nbsp;存',
                    width : 60,
                    handler : function() {
                        /*$('#imgEdit').form('submit', {
                            url:'/admin/editAppIdx',
                            ajax:true,
                            onSubmit: function(){
                                // 做一些检查
                                // 返回false可以阻止提交操作；
                            },
                            success:function(data){
                                if(data!='false'){
                                    $("#slide"+index).attr("src",data);//设置预览
                                    imgWindow.dialog('close');
                                    showMsg('设置成功！');
                                }else{
                                    showMsg('设置失败！');
                                }
                            }
                        });*/


                        var fd = new FormData();
                        fd.append("name", "图片");
                        fd.append("files", document.getElementById("inputImage").files[0]);

                        //scope.$apply();

                        var xhr = new XMLHttpRequest();
                        fd.append('dataX',$('#dataX').val());
                        fd.append('dataY',$('#dataY').val());
                        fd.append('dataWidth',$('#dataWidth').val());
                        fd.append('dataHeight',$('#dataHeight').val());
                        fd.append('oldidx',$('#oldidx').val());
                        fd.append('edittype',edittype);
                        xhr.open("POST" ,"/admin/editAppIdx" , true);

                        xhr.setRequestHeader('Authorization','Bearer ' +  localStorage.getItem('token'))//$window.sessionStorage.token

                        xhr.send(fd);
                        xhr.onload = function(e) {
                            if (this.status == 200) {
                                //文件上传完成
                                var result = this.responseText;
                                //alert(result);
                                if(result!='false'){
                                    if(edittype==1){
                                        $("#slide"+index).attr("src",'/javascripts/slider/img/sls/'+$('#oldidx').val()+'?random='+Math.random());//设置预览
                                    }else{
                                        $("#slide"+index).attr("src",'/javascripts/slider/img/sls1/'+$('#oldidx').val()+'?random='+Math.random());//设置预览
                                    }

                                    imgWindow.dialog('close');
                                    showMsg('设置成功！');
                                }else{
                                    showMsg('设置失败！');
                                }

                            }
                        }

                    }
                } ],
            onClose : function() {
                $(this).dialog('destroy');//动态加载一定要销毁
            },
            //加载数据，用onLoad，不是用onOpen
            onLoad : function() {
                /*$("#imgs").attr("src",'');//设置预览*/
                $('#oldidx').val(idxlist[index]);
            }
        });
}

function showMsg(msg){
    $.messager.show({
        title:'提示',
        msg:msg,
        timeout:2000,
        showType:'fade'
    });

}